package awd.bj.jls.webs.controller;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Maps;

import awd.bj.jls.model.LdspModel;
import awd.bj.jls.webs.api.JlsServersApi;
import awd.bj.jls.webs.api.model.User;
import awd.bj.jls.webs.utils.DefaultQueryParam;
import awd.bj.jls.webs.utils.PagerResult;
import awd.bj.jls.webs.utils.QueryParam;
import awd.bj.jls.webs.utils.ResponseMessage;
import awd.bj.jls.webs.utils.Result;
import awd.bj.jls.webs.utils.ResultUtils;
import awd.bj.jls.webs.utils.TermType;
@Controller
@RequestMapping("/jls_ldsp")
@RefreshScope
public class LdspController extends BaseAction{

	/**
	 * 
	@GetMapping("/ldsp")
    public ResponseMessage<PagerResult<LdspModel>> ldspQueryForPage(@RequestBody QueryParam param);

    @PostMapping("/ldsp")
    public ResponseMessage<String> ldspSave(@RequestBody LdspModel ldspEntity);

    @PutMapping("/ldsp/{id}")
    public String ldspUpdate(@RequestParam(value="id") String id ,@RequestBody LdspModel ldspEntity);

	 */
    @Autowired
    private JlsServersApi jlsServersApi;

    /**
     * 初始化模板
     *
     * @param model
     * @return
     */
    @RequestMapping("/index.html")
    public String ldspIndex(Model model) {
        User users = this.getCurrentUser();
        model.addAttribute("userName", users.getUsername());
        return "jls_ldsp/index";
    }
    @RequestMapping("/ldsp.html")
    public String ldsp(Model model) {
        User users = this.getCurrentUser();
        model.addAttribute("userName", users.getUsername());
        return "jls_ldsp/ldsp";
    }
        @RequestMapping("/ldspList")
        @ResponseBody
        public Map<String, Object> ldspList(HttpServletRequest request) {
        User users = this.getCurrentUser();
        QueryParam param = new QueryParam();
        String state = request.getParameter("state");
        String kssj=request.getParameter("kssj");
        String jssj=request.getParameter("jssj");
        DefaultQueryParam.addDefaultQueryParam(request, param, state);
        param.and("jsbh",TermType.eq,users.getJsbh());

            System.err.println("何磊:保存数据====================================");
            System.err.println(JSON.toJSONString(param));
            System.err.println("=================================================");

        ResponseMessage<PagerResult<LdspModel>> list = jlsServersApi.ldspQueryForPage(param);
        Map<String, Object> outmap = Maps.newHashMap();
        outmap.put("total", list.getResult().getTotal());
        outmap.put("rows", list.getResult().getData());
        return outmap;
        }
        @RequestMapping("/ldspAdd")
        @ResponseBody
        public Result<?> ldspAdd(@ModelAttribute LdspModel ldspModel) {
        User users = this.getCurrentUser();
        try {
        ldspModel.setState("R2");
        ldspModel.setJsbh(users.getJsbh());
        ldspModel.setCreator(users.getUsername());
        ldspModel.setCreatetime(new Date());

            System.err.println("何磊:保存数据====================================");
            System.err.println(JSON.toJSONString(ldspModel));
            System.err.println("================================================================");


        return ResultUtils.success(jlsServersApi.ldspSave(ldspModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }
        @RequestMapping("/ldspDelete")
        @ResponseBody
        public Result<?> ldspDelete(@ModelAttribute LdspModel ldspModel) {
        User users = this.getCurrentUser();
        try {
        ldspModel.setState("R3");
        ldspModel.setJsbh(users.getJsbh());
        ldspModel.setUpdator(users.getUsername());
        ldspModel.setUpdatetime(new Date());
        return ResultUtils.success(jlsServersApi.ldspUpdate(ldspModel.getId(),ldspModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }
        @RequestMapping("/ldspUpdate")
        @ResponseBody
        public Result<?> ldspUpdate(@ModelAttribute LdspModel ldspModel) {
        User users = this.getCurrentUser();
        try {
        ldspModel.setState("R2");
        ldspModel.setJsbh(users.getJsbh());
        ldspModel.setUpdator(users.getUsername());
        ldspModel.setUpdatetime(new Date());
        return ResultUtils.success(jlsServersApi.ldspUpdate(ldspModel.getId(),ldspModel));
        } catch (Exception e) {
        e.printStackTrace();
        return ResultUtils.error(Result.ERR_SAVE, "保存失败");
        }
        }

        
       
}
